package com.leancloud.circle.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSONArray;
import com.aries.ui.view.radius.RadiusEditText;
import com.aries.ui.view.radius.RadiusLinearLayout;
import com.aries.ui.view.radius.RadiusTextView;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVObject;
import com.leancloud.circle.R;
import com.leancloud.circle.adapter.CircleCommentAdapter;
import com.leancloud.circle.adapter.CirclePhotoAdapter;
import com.leancloud.circle.bean.CircleBean;
import com.leancloud.circle.bean.CollectionBean;
import com.leancloud.circle.bean.CommentBean;
import com.leancloud.circle.manage.CircleBack;
import com.leancloud.circle.manage.CircleGlobal;
import com.leancloud.circle.manage.CirclePathManage;

import java.util.List;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.cache.CacheData;
import tech.com.commoncore.manager.GlideManager;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.ToastUtil;

@Route(path = CirclePathManage.CIRCLE_TOPIC_DETAILS)
public class TopicDetailsActivity extends BaseTitleActivity implements View.OnClickListener{

    private ImageView headView;
    private TextView tv_title,userNameTx, dateTx,contentTx, collectionTx, commentTx, likeTx, browseTx;
    private int circleId;
    private LinearLayout communityContentLayout;

    private CircleCommentAdapter commentAdapter;
    private RecyclerView commitRecyclerView;

    private FrameLayout commentInputLt;
    private RadiusTextView commentSubmitTx;
    private InputMethodManager inputMethodManager;
    private RadiusEditText commentEt;
    private RadiusLinearLayout mRllComment;

    private int c_id=0;
    private TextView tvJ1;
    private TextView tvJ2;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("正文");
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_toipc_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        headView = findViewById(R.id.circle_details_user_head);
        userNameTx = findViewById(R.id.circle_details_user_name);
        dateTx = findViewById(R.id.circle_details_date);
        tv_title = findViewById(R.id.tv_title);
        contentTx = findViewById(R.id.circle_details_content);
        collectionTx = findViewById(R.id.circle_details_collection);
        commentTx = findViewById(R.id.circle_details_comment);
        likeTx = findViewById(R.id.circle_details_like);
        browseTx = findViewById(R.id.circle_details_browse_tx);
        mRllComment = findViewById(R.id.rll_comment);
        commitRecyclerView = findViewById(R.id.circle_details_commit_recycler);
        communityContentLayout = findViewById(R.id.community_content_layout);

        commentInputLt = mContentView.findViewById(R.id.comment_input_layout);
        commentEt = mContentView.findViewById(R.id.comment_input_content);
        commentSubmitTx = mContentView.findViewById(R.id.comment_input_submit);
        inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

        circleId = getIntent().getIntExtra("communityId", 0);
        if (circleId != 0) {
            requestData();
        }

        commentAdapter = new CircleCommentAdapter(R.layout.circle_layout_item_comment);
        commitRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        commitRecyclerView.setAdapter(commentAdapter);

        commentSubmitTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String comment = commentEt.getText().toString();
                if (comment.isEmpty()) {
                    ToastUtil.show("内容不能为空");
                }
            }
        });


        tvJ1 = (TextView) findViewById(R.id.tv_j_1);
        tvJ2 = (TextView) findViewById(R.id.tv_j_2);
    }


    private void requestData() {
        CircleGlobal.getCircleImp().requestCircleDetails(circleId, new CircleBack<CircleBean>() {
            @Override
            public void onBack(CircleBean bean, String err) {
                if (err == null && bean != null) {
                    setViewData(bean);
                    communityContentLayout.setVisibility(View.VISIBLE);
                } else {
                    communityContentLayout.setVisibility(View.GONE);
                }
            }
        });
        CircleGlobal.getCircleImp().requestComment(1, 20, null, circleId, -1, new CircleBack<List<CommentBean>>() {
            @Override
            public void onBack(List<CommentBean> beans, String err) {
                if (err == null && beans != null) {
                    commentAdapter.replaceData(beans);
                    commentAdapter.notifyDataSetChanged();
                }
            }
        });

    }

    private void setViewData(final CircleBean bean) {
        c_id = bean.getId();

        String nike = bean.getUser().getUser_nickname();
        String headUrl = bean.getUser().getAvatar();

        String date = bean.getCreate_time();
        String content = bean.getPost_content();
        String postTitle = bean.getPost_title();
        List<String> images = JSONArray.parseArray(bean.getMore(), String.class);
        String commentCount = String.valueOf(bean.getComment_count());
        String likeCount = String.valueOf(bean.getPost_like());
        String browseCount = String.valueOf(bean.getPost_hits());

        GlideManager.loadCircleImg(headUrl, headView, R.mipmap.circle_head);

        RecyclerView imagesRecycler = findViewById(R.id.circle_details_image_recycler);
        CirclePhotoAdapter photoAdapter = new CirclePhotoAdapter(R.layout.circle_layout_item_circle_photo);
        imagesRecycler.setAdapter(photoAdapter);
        imagesRecycler.setLayoutManager(new GridLayoutManager(mContext, 3));
        if (images !=null && !images.isEmpty()){
            photoAdapter.replaceData(images);
            photoAdapter.notifyDataSetChanged();
        }


        userNameTx.setText(nike);
        dateTx.setText(date);
        contentTx.setText(content);
        collectionTx.setText("");
        if (postTitle == null || postTitle.isEmpty()){
            tvJ1.setVisibility(View.GONE);
            tvJ2.setVisibility(View.GONE);
        }else {
            tvJ1.setVisibility(View.VISIBLE);
            tvJ2.setVisibility(View.VISIBLE);
        }
        tv_title.setText(postTitle);
        commentTx.setText(commentCount);
        likeTx.setText(likeCount);
        browseTx.setText(browseCount);


        collectionTx.setSelected(CircleGlobal.isCollections(bean.getId()));
        mRllComment.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rll_comment) {
            if (ServiceFactory.getInstance().getLoginService().getCurrentUser() !=null) {
                startActivityForResult(CommentActivity.actionToActivity(mContext, c_id), 123);
            } else {
                ARouter.getInstance().build("/login/login").navigation();
            }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123 && resultCode == RESULT_OK) {
            requestData();

        }
    }
}
